home *** CD-ROM | disk | FTP | other *** search
/ The Fatted Calf / The Fatted Calf.iso / Applications / Graphics / GraphicsWorkshop / Source / Converters / tif.m < prev    next >
Encoding:
Text File  |  1992-05-24  |  5.5 KB  |  266 lines

  1. #import <stdio.h>
  2. #import <strings.h>
  3. #import <streams/streams.h>
  4. #import <appkit/NXImage.h>
  5. #import <appkit/NXBitmapImageRep.h>
  6. #import <appkit/View.h>
  7. #import <appkit/Box.h>
  8. #import <appkit/Cell.h>
  9. #import <appkit/PopUpList.h>
  10. #import <appkit/Button.h>
  11. #import <appkit/TextField.h>
  12. #import <appkit/Text.h>
  13. #import <appkit/graphics.h>
  14. #import <appkit/tiff.h>
  15. #import "tif.h"
  16.  
  17. @implementation TIF
  18.  
  19.     id        myPopUp;                    // Pop up list for compression.
  20.     id        myTextIII;                    // Will hold compression factor.
  21.     int        compression;                    // Compression method to use.
  22.     float        factor;                        // Factor to compress by.
  23.  
  24. - init
  25. {
  26.     compression = NX_TIFF_COMPRESSION_NONE;
  27.     factor = 10.0;
  28.     
  29. #ifdef DEBUG
  30.     fprintf(stderr, "Tiff module initialized\n");
  31. #endif
  32.     
  33.     return self;
  34. }
  35.  
  36. - free
  37. {
  38.     return self;
  39. }
  40.  
  41. - readFromStream: (NXStream *)stream from: sender;
  42. {
  43. #ifdef DEBUG
  44.     fprintf(stderr, "Made it to read\n");
  45. #endif
  46.  
  47.     return [[NXBitmapImageRep alloc] initFromStream: stream];
  48. }
  49.  
  50. - (BOOL)write: (id)image toStream: (NXStream *)stream from: sender;
  51. {
  52.     if ([image bitsPerSample] < 4) {
  53.         if (compression == NX_TIFF_COMPRESSION_JPEG) {
  54.             compression = NX_TIFF_COMPRESSION_LZW;
  55. #ifdef DEBUG
  56.             fprintf(stderr, "Compress dropped from JPEG to LZH\n");
  57. #endif
  58.         }
  59.     }
  60. #ifdef DEBUG
  61.     fprintf(stderr, "compression %d and factor %.2f\n", compression, factor);
  62. #endif
  63.     [image     writeTIFF: stream 
  64.             usingCompression: compression
  65.             andFactor: factor];
  66.  
  67.     return YES;
  68. }
  69.  
  70. - popClick
  71. {
  72.     const char        *selection;
  73.     
  74.     selection = [myPopUp selectedItem];
  75.     if (!strcmp(selection, "None")) {
  76.         compression = NX_TIFF_COMPRESSION_NONE;
  77.     }
  78.     else if (!strcmp(selection, "LZW")) {
  79.         compression = NX_TIFF_COMPRESSION_LZW;
  80.     }
  81.     else if (!strcmp(selection, "Pack Bits")) {
  82.         compression = NX_TIFF_COMPRESSION_PACKBITS;
  83.     }
  84.     else if (!strcmp(selection, "JPEG")) {
  85.         compression = NX_TIFF_COMPRESSION_JPEG;
  86.     }
  87. #ifdef DEBUG
  88.     fprintf(stderr, "Compress %s with factor %.2f\n", selection, factor);
  89. #endif
  90.     
  91.     return self;
  92. }
  93.  
  94. - readAllFromStream: (NXStream *)stream from: sender
  95. {
  96. #ifdef DEBUG
  97.     fprintf(stderr, "Made it to read multiple\n");
  98. #endif
  99.  
  100.     return [[NXImage alloc] initFromStream: stream];
  101. }
  102.  
  103. - (BOOL)writeAll: (id)image toStream: (NXStream *)stream
  104. {
  105. #ifdef DEBUG
  106.     fprintf(stderr, "Made it to write multiple\n");
  107. #endif
  108.     
  109.     [image writeTIFF: stream allRepresentations: YES];
  110.  
  111.     return YES;
  112. }
  113.  
  114. - customSaveView: (int)width
  115. {
  116.     id        myBox        = [Box alloc];
  117.     id        myView        = [View alloc];
  118.     id        myButton     = [Button alloc];
  119.     id        myText        = [TextField alloc];
  120.     id        myTextII        = [TextField alloc];
  121.     NXRect    myViewRect     = {0, 0, width - 10, 30 };
  122.     NXRect    myButRect     = {width - 185, 5,   80, 21 };
  123.     NXRect    myTextRect     = {4, 6, width - 195, 18 };
  124.     NXRect    myTextRectII = {width - 105, 6, 45, 18 };
  125.     NXRect    myTextRectIII = {width - 55, 5, 40, 21 };
  126.  
  127.     [myBox initFrame: &myViewRect];
  128.         
  129.     [myView initFrame: &myViewRect];
  130.  
  131.     myPopUp  = [PopUpList alloc];
  132.     [myPopUp init];
  133.     [myPopUp addItem: "None"];
  134.     [myPopUp addItem: "LZW"];
  135.     [myPopUp addItem: "Pack Bits"];
  136.     [myPopUp addItem: "JPEG"];
  137.     [myPopUp setAction: @selector(popClick)];
  138.     [myPopUp setTarget: self];
  139.  
  140.     [myButton initFrame: &myButRect];
  141.     NXAttachPopUpList(myButton, myPopUp);
  142.     [myView addSubview: myButton];
  143.     [myButton setTitle: "None"];
  144.  
  145.     [myText initFrame: &myTextRect];
  146.     [myText setEditable: NO];
  147.     [myText setStringValue: "Compression"];
  148.     [myText setBezeled: NO];
  149.     [myText setAlignment: NX_RIGHTALIGNED];
  150.     [myText setBackgroundGray: NX_LTGRAY];
  151.     [myView addSubview: myText];
  152.  
  153.     [myTextII initFrame: &myTextRectII];
  154.     [myTextII setEditable: NO];
  155.     [myTextII setStringValue: "Factor"];
  156.     [myTextII setBezeled: NO];
  157.     [myTextII setAlignment: NX_RIGHTALIGNED];
  158.     [myTextII setBackgroundGray: NX_LTGRAY];
  159.     [myView addSubview: myTextII];
  160.  
  161.     myTextIII = [TextField alloc];
  162.     [myTextIII initFrame: &myTextRectIII];
  163.     [myTextIII setEditable: YES];
  164.     [myTextIII setFloatValue: factor];
  165.     [myTextIII setFloatingPointFormat: NO left: 0 right: 2];
  166.     [myTextIII setBezeled: YES];
  167.     [myTextIII setAlignment: NX_CENTERED];
  168.     [myTextIII setBackgroundGray: NX_WHITE];
  169.     [myTextIII  setTextDelegate: self];
  170.     [myView addSubview: myTextIII];
  171.  
  172.     [myBox setContentView: myView];
  173.     [myBox setTitle: "TIFF Options"];
  174.     [myBox sizeToFit];
  175.  
  176.     return myBox;
  177. }
  178.  
  179. - customOpenView: (int)width
  180. {
  181.     return nil;
  182. }
  183.  
  184. - (char *)getFormatName
  185. {
  186.     return("Tagged Image File Format (TIFF)");
  187. }
  188.  
  189. - textDidEnd: textObject endChar: (unsigned short)whyEnd
  190. {
  191.     factor = [myTextIII floatValue];
  192.     if (factor < 1.0) factor = 1.0;
  193.     if (factor > 255.0) factor = 255.0;
  194.  
  195.     [myTextIII setFloatValue: factor];
  196.  
  197. #ifdef DEBUG
  198.     fprintf(stderr, "Editing ended\n");
  199. #endif
  200.     return self;
  201. }
  202.  
  203. - (BOOL)setCustomParameter: (const char *)parameter withValue: (void *)ptr
  204. {
  205.     if (!strcmp(parameter,  TIFF_COMPRESS_METHOD)) {
  206.         compression = *(int *)ptr;
  207.         return YES;
  208.     }
  209.     else if (!strcmp(parameter, TIFF_COMPRESS_RATIO)) {
  210.         float        tmpFactor;
  211.         
  212.         tmpFactor = *(float *)ptr;
  213.         if ((tmpFactor < 1.0) || (tmpFactor > 255.0)) return NO;
  214.         factor = tmpFactor;
  215.         return YES;
  216.     }
  217.     else {
  218.         return NO;
  219.     }
  220. }
  221.  
  222. - (void *)getCustomParameter: (const char *)parameter
  223. {
  224.     if (!strcmp(parameter,  TIFF_COMPRESS_METHOD)) {
  225.         return &compression;
  226.     }
  227.     else if (!strcmp(parameter, TIFF_COMPRESS_RATIO)) {
  228.         return &factor;
  229.     }
  230.     else {
  231.         return nil;
  232.     }
  233. }
  234.  
  235. - (char *)copyrightNotice
  236. {
  237.     return "TIFF Converter\nby Alex Raftis\nCopyright (c) 1991 Cal Poly State University\nCopyright (c) 1986-1991 NeXT Computer Inc.\nEmail bugs to alex@data.ACS.CalPoly.EDU";
  238. }
  239.  
  240. - (int)errorState
  241. {
  242.     return CONVERT_ERR_NONE;
  243. }
  244.  
  245. - (int)errorMessage
  246. {
  247.     return ERROR_NO_ERROR;
  248. }
  249.  
  250. - (char *)errorStringMessage
  251. {
  252.     return NULL;
  253. }
  254.  
  255. - (BOOL)needsWindowServer;
  256. {
  257.     return NO;
  258. }
  259.  
  260. - (char *)protocolVersion
  261. {
  262.     return "1.0";
  263. }
  264.  
  265. @end
  266.